package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private TanhuaService tanhuaService;
    @Autowired
    private UserService userService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更改用户头像
     * return : null
     * in : headPhoto
     */
    @PostMapping("/header")
    public ResponseEntity updateHeadPhoto(@RequestBody MultipartFile headPhoto) throws IOException {
        Long userId = UserHolder.getUserId();
        userInfoService.updateHeadPhoto(userId, headPhoto);
        return ResponseEntity.ok(null);
    }

    /**
     * 喜欢列表
     *
     * @param type
     * @param page
     * @param pagesize
     * @param nickname
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity getLikeList(@PathVariable("type") String type,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize,
                                      String nickname) {
        PageResult pr = tanhuaService.queryLikeList(type, page, pagesize, nickname);

        return ResponseEntity.ok(pr);
    }

    /**
     * 修改手机--发送验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        String phone = UserHolder.getMobile();
        userService.sendMsg(phone);

        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机--校验验证码
     *
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String verificationCode = (String) map.get("verificationCode");
        String phone = UserHolder.getMobile();

        boolean verification = userInfoService.checkVerificationCode(phone, verificationCode);
        map.put("verification", verification);
        return ResponseEntity.ok(map);
    }

    /**
     * 修改手机--保存新的手机号
     *
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map) {
        String phone = (String) map.get("phone");

        userService.update(phone);

        return ResponseEntity.ok(null);
    }


}
